﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using TrafficArchives.Common;
using TrafficArchives.DataAccess;
using TrafficArchives.Logs;
using TrafficArchives.Models;

namespace TrafficArchives.Users
{
    public partial class UserView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initlize();

                QueryData();
            }
        }

        private void Initlize()
        {
            var dpDb = new DepartmentDb();
            var dpData = dpDb.GetData();
            DdlManageOrgan.DataSource = dpData;
            DdlManageOrgan.DataTextField = "treepath";
            DdlManageOrgan.DataValueField = "id";
            DdlManageOrgan.DataBind();

            var item = new ListItem("<请选择>", "");
            DdlManageOrgan.Items.Insert(0, item);
            DdlManageOrgan.SelectedIndex = 0;
        }

        private void QueryData()
        {
            var request = new Request { Pagination = { Enabled = true, PageSize = MainPager.PageSize, PageIndex = MainPager.CurrentPageIndex - 1 } };
            if (!string.IsNullOrEmpty(DdlIssuingOrgan.SelectedValue))
            {
                var con = new Condition
                {
                    FieldName = "FZJGNAME",
                    Operator = ConditionOperator.Like,
                    Values = new object[] { DdlIssuingOrgan.SelectedValue }
                };
                request.Items.Add(con);
            }
            if (!string.IsNullOrEmpty(DdlManageOrgan.SelectedValue))
            {
                var con = new Condition
                {
                    FieldName = "GLBMNAME",
                    Operator = ConditionOperator.Like,
                    Values = new object[] { DdlManageOrgan.SelectedValue }
                };
                request.Items.Add(con);
            }
            var db = new UserDb();
            var data = db.GetData(request);
            MainPager.RecordCount = data.RecordCount;
            GridViewMain.DataSource = data.Data;
            GridViewMain.DataBind();

            // 没数据时，显示表头。
            TableHeader.Visible = data.RecordCount == 0;
            UpdatePanelMain.Update();
        }

        protected void GridViewMainRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var dataItem = (DataRowView) e.Row.DataItem;

                if (dataItem["isinternal"].ToString() == "1")
                {
                    var btnModify = (HtmlGenericControl)e.Row.FindControl("DivModify");
                    var btnDel = (HtmlGenericControl)e.Row.FindControl("DivDel");
                    if (btnModify != null)
                    {
                        btnModify.Visible = false;
                    }
                    if (btnDel != null)
                    {
                        btnDel.Visible = false;
                    }
                }
            }
        }

        /// <summary>
        /// 数据处按钮处理。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridViewMainRowCommand(object sender, GridViewCommandEventArgs e)
        {
            var id = e.CommandArgument.ToString();
            var db = new UserDb();
            switch (e.CommandName)
            {
                case "Edit":
                    Response.Redirect(string.Format("AddUser.aspx?action=edit&id={0}", id));
                    break;
                case "Del":
                    db.Delete(new List<string> { id });
                    QueryData();
                    break;
                case "InitPassword":
                    db.ResetPassword(id);
                    var log = new LogInfo(LogType.初始化密码);
                    LogWriter.Writer(log);
                    break;
            }
        }

        protected void MainPagerPageIndexChanged(object sender, EventArgs e)
        {
            QueryData();
        }

        protected void BtnQueryClicked(object sender, EventArgs e)
        {
            MainPager.CurrentPageIndex = 1;

            QueryData();
        }
    }
}